package com.gateway.security.common.matcher;

import com.gateway.common.constant.Constants;
import com.gateway.common.dto.ApiDefinition;
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * @program: api-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2025/2/10 22:51
 **/
public abstract class ApiDefintionServerWebExchangeMatcher implements ServerWebExchangeMatcher {

    @Override
    public Mono<MatchResult> matches(ServerWebExchange exchange) {
        ApiDefinition apiDefinition = Objects.requireNonNull(exchange.getAttribute(Constants.API_DATA));
        return match(apiDefinition) ? MatchResult.match() : MatchResult.notMatch();
    }

    /**
     * 是否匹配
     * @return
     */
    protected abstract boolean match(ApiDefinition apiDefinition);
}
